
package database;

import core.COINCOMOComponent;
import core.COINCOMOSubSystem;
import core.COINCOMOSystem;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;

import dialogs.SubSystemEAFDialog;

/**
 *
 * @author Raed Shomali
 */
public class COINCOMOSubSystemManager
{
    private static String insertSubSystemSQL( COINCOMOSubSystem subSystem )
    {
        StringBuffer sql = new StringBuffer();

        sql.append( "SELECT Insert_Subsystem(" );
        sql.append( "\'" + subSystem.getName() + "\'," );
        sql.append( "" + subSystem.getParentID() + ");" );

        return sql.toString();
    }

    public static COINCOMOSubSystem insertSubSystem( long systemID )
    {
        COINCOMOSubSystem subSystem = null;

        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

        // Insert Only When a Connection is available ..
        if ( connection != null )
        {
            try
            {
                subSystem = new COINCOMOSubSystem();
                subSystem.setName( "(Sub System)" );
                subSystem.setParentID( systemID );

                // SQL Insert
                String sql = insertSubSystemSQL( subSystem );

                // For Better Performance Insert Statements
                PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

                // Do the insertion ...
                ResultSet rs = preparedStatement.executeQuery();

                try
                {
                    // Get Row
                    if ( rs.next() )
                    {
                        subSystem.setUnitID( rs.getInt( 1 ) );
                    }
                }
                catch ( SQLException se )
                {
                    return null;
                }
                finally
                {
                    // Release from memory ...
                    preparedStatement.close();
                }
            }
            catch ( SQLException e )
            {
                return null;
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
        }

        return subSystem;
    }

    public static boolean updateSubSystem( COINCOMOSubSystem subSystem )
    {
        boolean isSuccessful = false;

        // If Exists
        if ( subSystem != null )
        {
            DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

            if ( connection != null )
            {
                try
                {
                    String sql = "update cocomo_SubSystems_table set " +
                            "SubSystem_Name = ? " +
                            "where SubSystem_ID=?";

                    // Efficient Way of Updating
                    PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

                    // Replace "?" With Respective Values ..
                    preparedStatement.setString( 1 , subSystem.getName() );

                    preparedStatement.setLong( 2 , subSystem.getUnitID() );

                    // Update
                    preparedStatement.executeUpdate();

                    // Free From Memory
                    preparedStatement.close();

                    // Update Status
                    isSuccessful = true;
                }
                catch ( SQLException e )
                {
                    // Print Any Problems ..
                    e.printStackTrace();
                }
                finally
                {
                    // Disconnect
                    COINCOMODatabaseManager.disconnect( connection );
                }
            }
        }

        return isSuccessful;
    }

    //Code by Arnab.....
    public static boolean updateCosysmoCost(COINCOMOSubSystem subSystem)
    {
        boolean isSuccessful = false;
        //subSystem.get
        String costfactor[] = new String[14];
        costfactor=subSystem.getCosysmoRatings();
       
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
                String sql ="Select set_save_cosysmo_cost_parameter(1,'"+ costfactor[0]+"','"+ costfactor[1]+"','"+ costfactor[2]+"','"+ costfactor[3]+"','"+ costfactor[4]+"','"+ costfactor[5]+"','"+ costfactor[6]+"','"+ costfactor[7]+"','"+ costfactor[8]+"','"+ costfactor[9]+"','"+ costfactor[10]+"','"+costfactor[11]+"','"+ costfactor[12]+"','"+ costfactor[13]+"')";
              //String sql ="select set_save_cosysmo_cost_parameter(1,'H','H','H','H','H','H','H','H','H','H','H','H','H','H')";
                // Efficent Deletion
                PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
                
                preparedStatement.executeQuery();
                //preparedStatement.executeUpdate();
                preparedStatement.close();

                // Update Status
                isSuccessful = true;
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean updateCosysmoSize_nonreuse(COINCOMOSubSystem subSystem)
    {
        boolean isSuccessful = false;
        //subSystem.get
        float size_nonreuse[] = new float[12];
        size_nonreuse=subSystem.get_size_param_nonreuse();
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
                //String sql ="Select set_save_cosysmo_cost_parameter(1,'"+ costfactor[0]+"','"+ costfactor[1]+"','"+ costfactor[2]+"','"+ costfactor[3]+"','"+ costfactor[4]+"','"+ costfactor[5]+"','"+ costfactor[6]+"','"+ costfactor[7]+"','"+ costfactor[8]+"','"+ costfactor[9]+"','"+ costfactor[10]+"','"+costfactor[11]+"','"+ costfactor[12]+"','"+ costfactor[13]+"')";
                String sql= "select set_size_parameter(1,"+size_nonreuse[0]+","+size_nonreuse[1]+","+size_nonreuse[2]+","+size_nonreuse[3]+","+size_nonreuse[4]+","+size_nonreuse[5]+","+size_nonreuse[6]+","+size_nonreuse[7]+","+size_nonreuse[8]+","+size_nonreuse[9]+","+size_nonreuse[10]+","+size_nonreuse[11]+")";
                // Efficent Deletion
                PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
                
                preparedStatement.executeQuery();
                //preparedStatement.executeUpdate();
                preparedStatement.close();

                // Update Status
                isSuccessful = true;
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }   
        }
        return isSuccessful;
    }
    
    public static boolean updateCosysmoSize_reuse(COINCOMOSubSystem subSystem)
    {
        boolean isSuccessful = false;
        //subSystem.get
        float size_reuse[] = new float[72];
        size_reuse=subSystem.get_size_param_reuse();
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
                String sql1= "select set_size_parameter_reusable(1,"+size_reuse[0]+","+size_reuse[1]+","+size_reuse[2]+","+size_reuse[3]+","+size_reuse[4]+","+size_reuse[5]+","+size_reuse[6]+","+size_reuse[7]+","+size_reuse[8]+","+size_reuse[9]+","+size_reuse[10]+","+size_reuse[11]+","+size_reuse[12]+","+size_reuse[13]+","+size_reuse[14]+","+size_reuse[15]+","+size_reuse[16]+","+size_reuse[17]+",'sys_req')";
                String sql2= "select set_size_parameter_reusable(1,"+size_reuse[18]+","+size_reuse[19]+","+size_reuse[20]+","+size_reuse[21]+","+size_reuse[22]+","+size_reuse[23]+","+size_reuse[24]+","+size_reuse[25]+","+size_reuse[26]+","+size_reuse[27]+","+size_reuse[28]+","+size_reuse[29]+","+size_reuse[30]+","+size_reuse[31]+","+size_reuse[32]+","+size_reuse[33]+","+size_reuse[34]+","+size_reuse[35]+",'sys_interface')";
                String sql3= "select set_size_parameter_reusable(1,"+size_reuse[36]+","+size_reuse[37]+","+size_reuse[38]+","+size_reuse[39]+","+size_reuse[40]+","+size_reuse[41]+","+size_reuse[42]+","+size_reuse[43]+","+size_reuse[44]+","+size_reuse[45]+","+size_reuse[46]+","+size_reuse[47]+","+size_reuse[48]+","+size_reuse[49]+","+size_reuse[50]+","+size_reuse[51]+","+size_reuse[52]+","+size_reuse[53]+",'algo')";
                String sql4= "select set_size_parameter_reusable(1,"+size_reuse[54]+","+size_reuse[55]+","+size_reuse[56]+","+size_reuse[57]+","+size_reuse[58]+","+size_reuse[59]+","+size_reuse[60]+","+size_reuse[61]+","+size_reuse[62]+","+size_reuse[63]+","+size_reuse[64]+","+size_reuse[65]+","+size_reuse[66]+","+size_reuse[67]+","+size_reuse[68]+","+size_reuse[69]+","+size_reuse[70]+","+size_reuse[71]+",'operational_req')";
                // Efficent Deletion
                PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql1 );
                preparedStatement.executeQuery();
                
                preparedStatement = (PreparedStatement) connection.prepareStatement( sql2 );
                preparedStatement.executeQuery();
                
                preparedStatement = (PreparedStatement) connection.prepareStatement( sql3 );
                preparedStatement.executeQuery();
                
                preparedStatement = (PreparedStatement) connection.prepareStatement( sql4 );
                preparedStatement.executeQuery();
                //preparedStatement.executeUpdate();
                preparedStatement.close();

                // Update Status
                isSuccessful = true;
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }   
        }
        return isSuccessful;
    }
    
    //Code by Vishesh, Sheryl, Deepak, Ankoor.........
    
    public static boolean insertCocotProject( COINCOMOSubSystem subSystem )
    {
    	boolean isSuccessful = false;
    	deleteTempCocotsTables();

    	// If Exists
    	if ( subSystem != null )
    	{
    		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

    		if ( connection != null )
    		{
    			try
    			{
    				String sql = "insert into public.cocots_projects_table (project_name,subsystem_id,originator,proj_date) values ('" + subSystem.getProjectName() +  
                    "'," + subSystem.getUnitID() + ",'" + subSystem.getProjectOriginator() + 
                    "','" + subSystem.getDate() +"');";
                    String sql2 = "select cocots_get_proj_id();";
                    String sqlProjectID = "select * from cocots_temp;";
                    

                    // Efficient Way of Updating
                    PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
                    PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sql2 );
                    PreparedStatement preparedStatementProjectID = (PreparedStatement) connection.prepareStatement(sqlProjectID);
                    

                    // Update
                    preparedStatement.executeUpdate();
                    preparedStatement2.executeQuery();
                    ResultSet rs = preparedStatementProjectID.executeQuery();
                    
                    while ( rs != null && rs.next() )
                    {
                    	rs.getInt(1);
                        rs.getString(1);
                        subSystem.setProjectID(rs.getInt(1));
                    }
                    

                    // Free From Memory
                    preparedStatement.close();
                    preparedStatement2.close();
                    preparedStatementProjectID.close();


    				// Update Status
    				isSuccessful = true;
    			}
    			catch ( SQLException e )
    			{
    				// Print Any Problems ..
    				e.printStackTrace();
    			}
    			finally
    			{
    				// Disconnect
    				COINCOMODatabaseManager.disconnect( connection );
    			}
    		}
    	}

    	return isSuccessful;
    }
    
    public static boolean insertCocotsCots( COINCOMOSubSystem subSystem )
    {
    	boolean isSuccessful = false;
    	// If Exists
    	if ( subSystem != null )
    	{
    		DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

    		if ( connection != null )
    		{
    			try
    			{
    				ArrayList<Integer> cotIDs = subSystem.getCotClassID();
    				for ( Integer cotID : cotIDs )
    				{
    					String sql = "select cocots_insert_cots(" + cotID.intValue() + ");";
	
	                    // Efficient Way of Updating
	                    PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
	                                      
	
	                    // Update
	                    preparedStatement.executeQuery();
	                    
	
	                    // Free From Memory
	                    preparedStatement.close();


	    				// Update Status
	    				isSuccessful = true;
    				}
    			}
    			catch ( SQLException e )
    			{
    				// Print Any Problems ..
    				e.printStackTrace();
    			}
    			finally
    			{
    				// Disconnect
    				COINCOMODatabaseManager.disconnect( connection );
    			}
    		}
    	}

    	return isSuccessful;
    }
    
    public static boolean insertGlueCost ( COINCOMOSubSystem subSystem )
    {
    	boolean isSuccessful = false;
        //subSystem.get
        HashMap< Integer, int[] > costfactormap;
        costfactormap=subSystem.getCocotsRatings();
        int i = 0;
       
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
        	   Iterator it = costfactormap.entrySet().iterator();
        	   
        	   while(it.hasNext())
        	   {
        		   Map.Entry pairs = (Map.Entry)it.next();
        		   int cot_id = ((Integer)pairs.getKey()).intValue();
        		   int[] costfactor = (int[]) pairs.getValue();
        		   
        		   
        		   String sqlInsert ="select cocots_glue_insert_inputs_func(" + cot_id + ","+ costfactor[0]+","+ costfactor[1]+","
        		   + costfactor[2]+","+ costfactor[3]+","+ costfactor[4]+","+ costfactor[5]+","
        		   + costfactor[6]+","+ costfactor[7]+","+ costfactor[8]+","+ costfactor[9]+","+costfactor[10]+","
        		   + subSystem.getKSLOC() + "," + subSystem.getCREVOL() + ");";
        		   
                   String sqlComp = "select cocots_glue_insert_comp_func(" + cot_id + ","+ costfactor[11]+","+ costfactor[12]+
                   ","+ costfactor[13]+","+ costfactor[14]+","+ costfactor[15]+","+ costfactor[16] + ");";
                   
                   String sqlCompFunc = "select cocots_glue_complexity_func(" + cot_id + ");";
                   
                   
	                // Efficent Deletion
	                PreparedStatement preparedStatement1 = (PreparedStatement) connection.prepareStatement( sqlInsert );
	                PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sqlComp );
	                PreparedStatement preparedStatement3 = (PreparedStatement) connection.prepareStatement( sqlCompFunc );
	                
	                
	                               
	                preparedStatement1.executeQuery();
	                preparedStatement2.executeQuery();
	                preparedStatement3.executeQuery();
	                
	                
	                preparedStatement1.close();
	                preparedStatement2.close();
	                preparedStatement3.close();
	                
	                
	                
    				i++;
	                
	               
        	   }
        	   
        	   
        	   // Update Status
               isSuccessful = true;
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean insertAssessCost ( COINCOMOSubSystem subSystem )
    {
    	boolean isSuccessful = false;
        //subSystem.get
        HashMap< Integer, int[] > costfactormap;
        costfactormap=subSystem.getAssessCocotsRatings();
        int i = 0;
       
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
        	   Iterator it = costfactormap.entrySet().iterator();
        	   
        	   while(it.hasNext())
        	   {
        		   Map.Entry pairs = (Map.Entry)it.next();
        		   int cot_id = ((Integer)pairs.getKey()).intValue();
        		   int[] costfactor = (int[]) pairs.getValue();
        		   
        		   
        		   String sqlInsert ="select cocots_assess_insert_inputs_func(" + cot_id + "," + costfactor[17]+ "," + costfactor[18] +","
        		   + costfactor[0] + "," + costfactor[1] + ","
        		   + costfactor[2] + "," + costfactor[3] + "," + costfactor[4] + "," + costfactor[5]+","
        		   + costfactor[6] + "," + costfactor[7] + "," + costfactor[8] + "," + costfactor[9]+","
        		   + costfactor[10] + "," + costfactor[11] + "," + costfactor[12] + "," 
        		   + costfactor[13] + "," + costfactor[14] + "," + costfactor[15] + "," 
        		   + costfactor[16] + ");";
        		   
        		   
        		   
	                // Efficent Deletion
	                PreparedStatement preparedStatement1 = (PreparedStatement) connection.prepareStatement( sqlInsert );
	           
	                preparedStatement1.executeQuery();
	                
	                preparedStatement1.close();
	                
	                
    				i++;
	                
	               
        	   }
        	   
        	   
        	   // Update Status
               isSuccessful = true;
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean insertTailCost ( COINCOMOSubSystem subSystem )
    {
    	boolean isSuccessful = false;
        //subSystem.get
        HashMap< Integer, double[] > costfactormap;
        costfactormap= subSystem.getTailCocotsRatings();
        int i = 0;
       
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
        	   Iterator it = costfactormap.entrySet().iterator();
        	   
        	   while(it.hasNext())
        	   {
        		   Map.Entry pairs = (Map.Entry)it.next();
        		   int cot_id = ((Integer)pairs.getKey()).intValue();
        		   double[] costfactor = (double[]) pairs.getValue();
        		   
        		   
        		   String sqlInsert ="select cocots_tail_insert_inputs_func(" + cot_id + ","+ ((int)costfactor[6])+","+ (int)costfactor[0]+","
        		   + (int)costfactor[1]+","+ (int)costfactor[2]+","+ (int)costfactor[3]+","+ (int)costfactor[4]+","
        		   + (int)costfactor[5]+","+ (int)costfactor[6]+","+ costfactor[7] + ");";
        		   
                   String sqlTcq = "select cocots_tail_tcq_func(" + cot_id + ");";
                   
        		   
	                // Efficent Deletion
	                PreparedStatement preparedStatement1 = (PreparedStatement) connection.prepareStatement( sqlInsert );
	                PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sqlTcq );
	                
	                
	                               
	                preparedStatement1.executeQuery();
	                preparedStatement2.executeQuery();
	                
	                
	                preparedStatement1.close();
	                preparedStatement2.close();
	                
	                
	                
    				i++;
	                
	               
        	   }
        	   
        	   
        	   // Update Status
               isSuccessful = true;
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean insertCocotCost(COINCOMOSubSystem subSystem)
    {
    	boolean isSuccessful = false;
        //subSystem.get
        HashMap< Integer, int[] > costfactormap;
        costfactormap=subSystem.getCocotsRatings();
        int i = 0;
       
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
        	   Iterator it = costfactormap.entrySet().iterator();
        	   
        	   
        	   while(it.hasNext())
        	   {
        		   Map.Entry pairs = (Map.Entry)it.next();
        		   int cot_id = ((Integer)pairs.getKey()).intValue();
        		   int[] costfactor = (int[]) pairs.getValue();
        		   
        		   String sql1 ="select cocots_insert_cots(" + cot_id + ");";
        		   
        		   String sql2 ="select cocots_insert_inputs(" + cot_id + ","+ costfactor[0]+","+ costfactor[1]+","
        		   + costfactor[2]+","+ costfactor[3]+","+ costfactor[4]+","+ costfactor[5]+","
        		   + costfactor[6]+","+ costfactor[7]+","+ costfactor[8]+","+ costfactor[9]+","+costfactor[10]+","
        		   + subSystem.getKSLOC() + "," + subSystem.getCREVOL() + ");";
        		   
                   String sql3 = "select cocots_insert_comp(" + cot_id + ","+ costfactor[11]+","+ costfactor[12]+
                   ","+ costfactor[13]+","+ costfactor[14]+","+ costfactor[15]+","+ costfactor[16] + ");";
                   
                   String sql4 = "select cocots_complexity_func(" + cot_id + ");";
                   
        		   
	                // Efficent Deletion
	                PreparedStatement preparedStatement1 = (PreparedStatement) connection.prepareStatement( sql1 );
	                PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sql2 );
	                PreparedStatement preparedStatement3 = (PreparedStatement) connection.prepareStatement( sql3 );
	                PreparedStatement preparedStatement4 = (PreparedStatement) connection.prepareStatement( sql4 );
	                
	                
	                               
	                preparedStatement1.executeQuery();
	                preparedStatement2.executeQuery();
	                preparedStatement3.executeQuery();
	                preparedStatement4.executeQuery();
	                
	                
	                preparedStatement1.close();
	                preparedStatement2.close();
	                preparedStatement3.close();
	                preparedStatement4.close();
	                
	                
	                
    				i++;
	                
	               
        	   }
        	   
        	   
        	   // Update Status
               isSuccessful = true;
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean updateProjectResult(COINCOMOSubSystem subSystem)
    {
    	boolean isSuccessful = false;
       
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
        	   subSystem.clearCotClass();
        	   String sql5 = "select cocots_calc_func();";
        	   PreparedStatement preparedStatement5 = (PreparedStatement) connection.prepareStatement( sql5 );
        	   preparedStatement5.executeQuery();
        	   preparedStatement5.close();
                   
        	   String sql = "select proj_result from cocots_projects_table where project_id = " + subSystem.getProjectID() +";";
        	  
        	   PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

        	   ResultSet rs = preparedStatement.executeQuery();
        	   while ( rs != null && rs.next() )
        	   {
        		   subSystem.setProjectResult(rs.getDouble( 1 ));
        		   
        	   }

        	   //preparedStatement.executeUpdate();

        	   preparedStatement.close();
        	   
        	   String sql2 = "delete from cocots_temp";
        	   PreparedStatement preparedStatement2 = (PreparedStatement) connection.prepareStatement( sql2 );
        	   preparedStatement2.executeUpdate();
        	   preparedStatement2.close();




        	   // Update Status
        	   isSuccessful = true;
            
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean deleteTempCocotTables ()
    {
    	 boolean isSuccessful = false;

         DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

         // Check if connection exists
         if ( connection != null )
         {
             try
             {
                 String deleteCocotTemp = "delete from cocots_temp";
                 String deleteCocotGlueUpdate = "delete from cocots_glue_update_input_table";
                 String deleteCocotAssessUpdate = "delete from cocots_assess_update_input_table";
                 String deleteCocotTailUpdate = "delete from cocots_tail_update_input_table";

                 // Efficent Deletion
                 PreparedStatement preparedStatementDeleteCocotTemp = (PreparedStatement) connection.prepareStatement( deleteCocotTemp );
                 PreparedStatement preparedStatementDeleteCocotGlue = (PreparedStatement) connection.prepareStatement( deleteCocotGlueUpdate );
                 PreparedStatement preparedStatementDeleteCocotAssess = (PreparedStatement) connection.prepareStatement( deleteCocotAssessUpdate );
                 PreparedStatement preparedStatementDeleteCocotTail = (PreparedStatement) connection.prepareStatement( deleteCocotTailUpdate );

                 // Delete
                 preparedStatementDeleteCocotTemp.executeUpdate();
                 preparedStatementDeleteCocotGlue.executeUpdate();
                 preparedStatementDeleteCocotAssess.executeUpdate();
                 preparedStatementDeleteCocotTail.executeUpdate();

                 // Release From Memory
                 preparedStatementDeleteCocotTemp.close();
                 preparedStatementDeleteCocotGlue.close();
                 preparedStatementDeleteCocotAssess.close();
                 preparedStatementDeleteCocotTail.close();
                 

                 // Update Status
                 isSuccessful = true;
             }
             catch ( SQLException e )
             {
                 // Print any Problems ...
                 e.printStackTrace();
             }
             finally
             {
                 // Disconnect
                 COINCOMODatabaseManager.disconnect( connection );
             }
         }

         return isSuccessful;
    }
    
    public static boolean deleteCocotsTablesFinal()
    {
    	 boolean isSuccessful = false;

         DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

         // Check if connection exists
         if ( connection != null )
         {
             try
             {
                 String deleteCocotFinal = "select cocots_update_delete_all_func();";

                 // Efficent Deletion
                 PreparedStatement preparedStatementDeleteCocotFinal = (PreparedStatement) connection.prepareStatement( deleteCocotFinal );

                 // Delete
                 preparedStatementDeleteCocotFinal.executeQuery();

                 // Release From Memory
                 preparedStatementDeleteCocotFinal.close();
                 

                 // Update Status
                 isSuccessful = true;
             }
             catch ( SQLException e )
             {
                 // Print any Problems ...
                 e.printStackTrace();
             }
             finally
             {
                 // Disconnect
                 COINCOMODatabaseManager.disconnect( connection );
             }
         }

         return isSuccessful;
    }
    
    public static Vector<String> loadCocotsProjects()
    {
    	boolean isSuccessful = false;
       
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        Vector<String> projects = null;
        
        if ( connection != null )
        {
           try
            {
        	   String sql = "SELECT project_name FROM cocots_projects_table;";
        	  
        	   PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
        	   
        	   ResultSet rs = preparedStatement.executeQuery();
        	   projects = new Vector<String>();
        	   while ( rs != null && rs.next() )
        	   {
        		  projects.add(rs.getString(1));
        	   }

        	   preparedStatement.close();

        	   // Update Status
        	   isSuccessful = true;
            
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return projects;
    }
    
    public static boolean loadCocotProject ( String projectName , COINCOMOSubSystem subSystem )
    {
    	deleteTempCocotTables();
    	
    	boolean isSuccessful = false;
        
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
        	   String sql = "select cocots_get_update_proj_id_func('" + projectName+"');";
        	   String sqlProjectID = "SELECT temp FROM cocots_temp;";
		       // Efficient Way of Updating
		       PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
		       PreparedStatement preparedStatementProjID = (PreparedStatement) connection.prepareStatement( sqlProjectID );
		
		       ResultSet rs = preparedStatement.executeQuery();
		       ResultSet rsProjID = preparedStatementProjID.executeQuery();
		       
		       while ( rsProjID != null && rsProjID.next() )
               {
		    	   int projID = rsProjID.getInt( 1 );
		    	   subSystem.setProjectID(projID);
               }
		       
		       preparedStatement.close();
		       preparedStatementProjID.close();
		       
		       String sqlLoadCocotParams = "SELECT project_id, project_name, " +
		       		"subsystem_id, originator, proj_date, proj_result " +
		       		"FROM cocots_projects_table WHERE project_id = " + subSystem.getProjectID() + ";";
		       
		       PreparedStatement preparedStatementLoadParams = (PreparedStatement) connection.prepareStatement( sqlLoadCocotParams );
		       ResultSet rsLoadParams = preparedStatementLoadParams.executeQuery();
		       while ( rsLoadParams != null && rsLoadParams.next() )
               {
		    	   String projName = rsLoadParams.getString( 2 );
		    	   String origName = rsLoadParams.getString( 4 );
		    	   String date = rsLoadParams.getString(5 );
		    	   double projResult = rsLoadParams.getDouble( 6 );
		    	   
		    	   subSystem.setProjectName(projName);
		    	   subSystem.setProjectOriginator(origName);
		    	   subSystem.setDateString(date);
		    	   subSystem.setProjectResult(projResult);
		    	   
               }

		       preparedStatementLoadParams.close();
		       
		       String sqlUpdateSelect = "SELECT cocots_update_select_func();";
		       PreparedStatement preparedStatementUpdateSelect = (PreparedStatement) connection.prepareStatement( sqlUpdateSelect );
		       ResultSet rsUpdateSelect = preparedStatementUpdateSelect.executeQuery();
		       preparedStatementUpdateSelect.close();


        	   // Update Status
        	   isSuccessful = true;
            
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    	
    }
    
    public static boolean loadCocotGlue (COINCOMOSubSystem subSystem)
    {
    	boolean isSuccessful = false;
        
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
		       
		       String sqlLoadInputs = "SELECT project_id, cots_id, aciep, acipc, axicp, apcon, acpmt, acsew, " +
		       		"intconvap, ctrlap, datap, acpps, acptd, acrel, intconvaa, ctrlaa, dataa, " +
		       		"acper, asprt, ksloc, crevol " +
		       		"FROM cocots_glue_update_input_table;";
		       
		       PreparedStatement preparedStatementLoadInputs = (PreparedStatement) connection.prepareStatement( sqlLoadInputs );
		       ResultSet rsLoadInputs = preparedStatementLoadInputs.executeQuery();
		      
		       
		       while ( rsLoadInputs != null && rsLoadInputs.next() )
		       {
		    	   Integer cocot_id = new Integer(rsLoadInputs.getInt( 2 ));
		    	   if(cocot_id.intValue() == 0)
		    		   subSystem.setAggregateLoaded();
		    	   int ratings[] = new int[17];
		    	   
		    	   ratings[0] = rsLoadInputs.getInt( 3 ); //aciep
		    	   ratings[1] = rsLoadInputs.getInt( 4 ); //acipc
		    	   ratings[2] = rsLoadInputs.getInt( 5 ); //axicp
		    	   ratings[3] = rsLoadInputs.getInt( 6 ); //apcon
		    	   ratings[4] = rsLoadInputs.getInt( 7 ); //acpmt
		    	   ratings[5] = rsLoadInputs.getInt( 8 ); //acsew
		    	   
		    	   ratings[6] = rsLoadInputs.getInt( 9 ); //acpps
		    	   ratings[7] = rsLoadInputs.getInt( 10 ); //acptd
		    	   ratings[8] = rsLoadInputs.getInt( 11 ); //acrel
		    	   
		    	   ratings[9] = rsLoadInputs.getInt( 12 ); //acper
		    	   ratings[10] = rsLoadInputs.getInt( 13 ); //asprt
		    	   
		    	   ratings[11] = rsLoadInputs.getInt( 14 ); //apcpx
		    	   ratings[12] = rsLoadInputs.getInt( 15 ); //apcpx
		    	   ratings[13] = rsLoadInputs.getInt( 16 ); //apcpx
		    	   
		    	   ratings[14] = rsLoadInputs.getInt( 17 ); //apcpx
		    	   ratings[15] = rsLoadInputs.getInt( 18 ); //apcpx
		    	   ratings[16] = rsLoadInputs.getInt( 19 ); //apcpx
		    	   
		    	   subSystem.setKSLOC(rsLoadInputs.getDouble( 20 ));
		    	   subSystem.setCREVOL(rsLoadInputs.getDouble( 21 ));
		    	   
		    	  
		    	   subSystem.setLoadedCocotRatings(cocot_id, ratings);
		       }
		          
		       
		       
		       
		       
		       preparedStatementLoadInputs.close();


        	   // Update Status
        	   isSuccessful = true;
            
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean loadCocotAssess (COINCOMOSubSystem subSystem)
    {
    	boolean isSuccessful = false;
        
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
		       
		       String sqlLoadInputs = "SELECT project_id, cots_id, nocots_in, nocots_det, corr, avail, secur, " +
		       		"prod, under, eou, ver, intercomp, flex, install, port, func, " +
		       		"price, mat, vend, train, vendcon " +
		       		"FROM cocots_assess_update_input_table;";
		       
		       PreparedStatement preparedStatementLoadInputs = (PreparedStatement) connection.prepareStatement( sqlLoadInputs );
		       ResultSet rsLoadInputs = preparedStatementLoadInputs.executeQuery();
		      
		       
		       while ( rsLoadInputs != null && rsLoadInputs.next() )
		       {
		    	   Integer cocot_id = new Integer(rsLoadInputs.getInt( 2 ));
		    	   if(cocot_id.intValue() == 0)
		    		   subSystem.setAggregateLoaded();
		    	   int ratings[] = new int[19];
		    	   
		    	   //subSystem.setNumOfIntialAssessCots( rsLoadInputs.getInt( 3 ) ); //nocots_in
		    	   //subSystem.setNumOfFinalAssessCots( rsLoadInputs.getInt( 4 ) ); //nocots_det
		    	   
		    	   ratings[0] = rsLoadInputs.getInt( 5 ); //corr
		    	   ratings[1] = rsLoadInputs.getInt( 6 ); //avail
		    	   ratings[2] = rsLoadInputs.getInt( 7 ); //secur
		    	   ratings[3] = rsLoadInputs.getInt( 8 ); //prod
		    	   
		    	   ratings[4] = rsLoadInputs.getInt( 9 ); //under
		    	   ratings[5] = rsLoadInputs.getInt( 10 ); //eou
		    	   ratings[6] = rsLoadInputs.getInt( 11 ); //ver
		    	   
		    	   ratings[7] = rsLoadInputs.getInt( 12 ); //intercomp
		    	   ratings[8] = rsLoadInputs.getInt( 13 ); //flex
		    	   
		    	   ratings[9] = rsLoadInputs.getInt( 14 ); //install
		    	   ratings[10] = rsLoadInputs.getInt( 15 ); //port
		    	   ratings[11] = rsLoadInputs.getInt( 16 ); //func
		    	   
		    	   ratings[12] = rsLoadInputs.getInt( 17 ); //price
		    	   ratings[13] = rsLoadInputs.getInt( 18 ); //mat
		    	   ratings[14] = rsLoadInputs.getInt( 19 ); //vend
		    	   ratings[15] = rsLoadInputs.getInt( 20 ); //train
		    	   ratings[16] = rsLoadInputs.getInt( 21 ); //vendcon
		    	   
		    	   ratings[17] = rsLoadInputs.getInt( 3 ); //nocots_in
		    	   ratings[18] = rsLoadInputs.getInt( 4 ); //nocots_det
		    	   
		    	   
		    	  
		    	   subSystem.setLoadedAssessCocotRatings(cocot_id, ratings);
		       }
		          
		       
		       
		       
		       
		       preparedStatementLoadInputs.close();


        	   // Update Status
        	   isSuccessful = true;
            
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }

    public static boolean loadCocotTail (COINCOMOSubSystem subSystem)
    {
    	boolean isSuccessful = false;
        
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
		       
		       String sqlLoadInputs = "SELECT project_id, cots_id, cots_num, param_spec, script_w, io_report, " +
		       		"gui_ssl, sec_apis, avail_tools, tcq, tail_effort, median_tail_effort " +
		       		"FROM cocots_tail_update_input_table;";
		       
		       PreparedStatement preparedStatementLoadInputs = (PreparedStatement) connection.prepareStatement( sqlLoadInputs );
		       ResultSet rsLoadInputs = preparedStatementLoadInputs.executeQuery();
		      
		       
		       while ( rsLoadInputs != null && rsLoadInputs.next() )
		       {
		    	   Integer cocot_id = new Integer(rsLoadInputs.getInt( 2 ));
		    	   if(cocot_id.intValue() == 0)
		    		   subSystem.setAggregateLoaded();
		    	   double ratings[] = new double[10];
		    	   
		    	   subSystem.setNumOfTailCots( rsLoadInputs.getInt( 3 ) ); //cots_num
		    	   
		    	   ratings[0] = rsLoadInputs.getInt( 4 ); //param_spec
		    	   ratings[1] = rsLoadInputs.getInt( 5 ); //script_w
		    	   ratings[2] = rsLoadInputs.getInt( 6 ); //io_report
		    	   ratings[3] = rsLoadInputs.getInt( 7 ); //gui_ssl
		    	   
		    	   ratings[4] = rsLoadInputs.getInt( 8 ); //sec_apis
		    	   ratings[5] = rsLoadInputs.getInt( 9 ); //avail_tools
		    	   ratings[6] = rsLoadInputs.getInt( 10 ); //tcq
		    	   
		    	   ratings[7] = rsLoadInputs.getInt( 11 ); //tail_effort
		    	   
		    	   ratings[8] = rsLoadInputs.getInt( 3 );
		    	   ratings[9] = rsLoadInputs.getDouble( 12 );
		    	   
		    	   subSystem.setTailMedianDefault( rsLoadInputs.getInt( 12 ) ); //median_tail_effort
		    	   
		    	  
		    	   subSystem.setLoadedTailCocotRatings(cocot_id, ratings);
		       }
		          
		       
		       
		       
		       
		       preparedStatementLoadInputs.close();


        	   // Update Status
        	   isSuccessful = true;
            
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean loadMedianDefault ( int cot_id , COINCOMOSubSystem subSystem )
    {
    	boolean isSuccessful = false;
        
        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();
        if ( connection != null )
        {
           try
            {
		       
		       String sqlLoadInputs = "SELECT median_user_def " +
		       		"FROM cocots_tail_medtail_table " +
		       		"WHERE cots_id=" + cot_id + ";";
		       
		       PreparedStatement preparedStatementLoadInputs = (PreparedStatement) connection.prepareStatement( sqlLoadInputs );
		       ResultSet rsLoadInputs = preparedStatementLoadInputs.executeQuery();
		       
		       while ( rsLoadInputs != null && rsLoadInputs.next() )
		       {
		    	   subSystem.setTailMedianDefault( rsLoadInputs.getDouble( 1 ) );
		       }
		       
		       preparedStatementLoadInputs.close();


        	   // Update Status
        	   isSuccessful = true;
            
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
            
            
        }
       return isSuccessful;
    }
    
    public static boolean deleteTempCocotsTables ()
    {
    	 boolean isSuccessful = false;

         DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

         // Check if connection exists
         if ( connection != null )
         {
             try
             {
                 String deleteCocotTemp = "DELETE FROM cocots_temp";

                 // Efficent Deletion
                 PreparedStatement preparedStatementDeleteCocotTemp = (PreparedStatement) connection.prepareStatement( deleteCocotTemp );

                 // Delete
                 preparedStatementDeleteCocotTemp.executeUpdate();

                 // Release From Memory
                 preparedStatementDeleteCocotTemp.close();
                 
                 

                 // Update Status
                 isSuccessful = true;
             }
             catch ( SQLException e )
             {
                 // Print any Problems ...
                 e.printStackTrace();
             }
             finally
             {
                 // Disconnect
                 COINCOMODatabaseManager.disconnect( connection );
             }
         }

         return isSuccessful;
    }
    
    public static boolean deleteSubSystem( long subSystemID )
    {
        boolean isSuccessful = false;

        DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

        // Check if connection exists
        if ( connection != null )
        {
            try
            {
                String sql = "delete from cocomo_SubSystems_table where SubSystem_ID=?";

                // Efficent Deletion
                PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

                // Replace "?" With ID Value
                preparedStatement.setLong( 1 , subSystemID );

                // Delete
                preparedStatement.executeUpdate();

                // Release From Memory
                preparedStatement.close();

                // Update Status
                isSuccessful = true;
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
            }
            finally
            {
                // Disconnect
                COINCOMODatabaseManager.disconnect( connection );
            }
        }

        return isSuccessful;
    }
    
    public static boolean loadSubSystem( COINCOMOSubSystem subSystem , DBConnection connection )
    {
        // Check if a Connection is available ..
        if ( connection != null )
        {
            try
            {
                String sql = "Select " +
                            "project_Name , " +
                            "(scale_factor_button).scale_factor_base[ 1 ] ," +
                            "(scale_factor_button).scale_factor_inc[ 1 ] ," +
                            "(scale_factor_button).scale_factor_base[ 2 ]  ," +
                            "(scale_factor_button).scale_factor_inc[ 2 ]  ," +
                            "(scale_factor_button).scale_factor_base[ 3 ]  ," +
                            "(scale_factor_button).scale_factor_inc[ 3 ]  ," +
                            "(scale_factor_button).scale_factor_base[ 4 ] ," +
                            "(scale_factor_button).scale_factor_inc[ 4 ] ," +
                            "(scale_factor_button).scale_factor_base[ 5 ]  ," +
                            "(scale_factor_button).scale_factor_inc[ 5 ] ," +
                            "(schedule_button).post_arch_sched.base  , " +
                            "(schedule_button).post_arch_sched.incr , " +
                            "shift , " +
                            "project_id " +
                            "from cocomo_projects_table where subsystem_ID = ?";

                    // Efficient Way of Updating
                    PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

                    // Replace "?" With Respective Values ..
                    preparedStatement.setLong( 1 , subSystem.getUnitID() );
                    

                ResultSet rs = preparedStatement.executeQuery();

                // Check if a Row is returned ..
                while ( rs != null && rs.next() )
                {
                    String name = rs.getString( 1 );
                    
                    int scaleFactorsBase[] = new int[ 5 ] ;
                    int scaleFactorsIncr[] = new int[ 5 ] ;
                    
                    scaleFactorsBase[ 0 ] = rs.getInt( 2 ) ;
                    scaleFactorsIncr[ 0 ] = rs.getInt( 3 ) ;
                    scaleFactorsBase[ 1 ] = rs.getInt( 4 ) ;
                    scaleFactorsIncr[ 1 ] = rs.getInt( 5 ) ;
                    scaleFactorsBase[ 2 ] = rs.getInt( 6 ) ;
                    scaleFactorsIncr[ 2 ] = rs.getInt( 7 ) ;
                    scaleFactorsBase[ 3 ] = rs.getInt( 8 ) ;
                    scaleFactorsIncr[ 3 ] = rs.getInt( 9 ) ;
                    scaleFactorsBase[ 4 ] = rs.getInt( 10 ) ;
                    scaleFactorsIncr[ 4 ] = rs.getInt( 11 ) ;
                    
                    int scedBase = rs.getInt( 12 ) ;
                    int scedIncr = rs.getInt( 13 ) ;
                    
                    int shift = rs.getInt( 14 ) ;
                    
                    long componentID = rs.getLong( 15 ) ;
                    
                    // Create a component
                    COINCOMOComponent component = new COINCOMOComponent();

                    // Set Parameters
                    component.setUnitID( componentID  );
                    component.setParentID( subSystem.getUnitID() );
                    component.setName( name );
                    component.setScaleFactorsBase( scaleFactorsBase ) ;
                    component.setScaleFactorsIncr( scaleFactorsIncr ) ;
                    component.setScedBase( scedBase );
                    component.setScedIncr( scedIncr );
                    component.setMultiBuildShift( shift ) ;
                    
                    // Update Generated Formulas
                    component.setTotalProd( COINCOMOComponentManager.get_Total_Prod( component.getUnitID() , connection ) ) ;
                    component.setTotalCost( COINCOMOComponentManager.get_Total_Cost( component.getUnitID() , connection ) ) ;
                    component.setTotalInstCost( COINCOMOComponentManager.get_Total_Inst_Cost( component.getUnitID() , connection ) ) ;
                    component.setTotalStaff( COINCOMOComponentManager.get_Total_Staff( component.getUnitID() , connection ) ) ;
                    component.setTotalEffort( COINCOMOComponentManager.get_Total_Effort( component.getUnitID() , connection ) ) ;
                    component.setTotalSchedule( COINCOMOComponentManager.get_Total_Schedule( component.getUnitID() , connection ) ) ;
                    
                    component.setScaleFactor( COINCOMOComponentManager.get_Scale_Factor( component.getUnitID() , connection ) ) ;
                    
                    
                    sql = COINCOMOComponentManager.get_All_Phase_Effort_Percentage( component.getUnitID() ) ;
                    preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
                    ResultSet rs2 = preparedStatement.executeQuery() ;
                    
                    if( rs2 != null && rs2.next() )
                    {
                        component.setInceptionEffortPercentage( Double.parseDouble( rs2.getString( 1 ) == null ? "0" : rs2.getString( 1 ) ) );
                        component.setElaborationEffortPercentage( Double.parseDouble( rs2.getString( 2 ) == null ? "0" : rs2.getString( 2 ) ) );
                        component.setConstructionEffortPercentage( Double.parseDouble( rs2.getString( 3 ) == null ? "0" : rs2.getString( 3 ) ) );
                        component.setTransitionEffortPercentage( Double.parseDouble( rs2.getString( 4 ) == null ? "0" : rs2.getString( 4 ) ) );
                    }
                    
                    sql = COINCOMOComponentManager.get_All_Phase_Schedule_Percentage( component.getUnitID() ) ;
                    preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
                    rs2 = preparedStatement.executeQuery() ;
                    
                    if( rs2 != null && rs2.next() )
                    {
                        component.setInceptionSchedulePercentage( Double.parseDouble( rs2.getString( 1 ) == null ? "0" : rs2.getString( 1 ) ) ) ;
                        component.setElaborationSchedulePercentage( Double.parseDouble( rs2.getString( 2 ) == null ? "0" : rs2.getString( 2 ) ) );
                        component.setConstructionSchedulePercentage( Double.parseDouble( rs2.getString( 3 ) == null ? "0" : rs2.getString( 3 ) ) );
                        component.setTransitionSchedulePercentage( Double.parseDouble( rs2.getString( 4 ) == null ? "0" : rs2.getString( 4 ) ) );
                    }
                    
                    
                    sql = COINCOMOComponentManager.get_All_Phase_Effort( component.getUnitID() ) ;
                    preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
                    rs2 = preparedStatement.executeQuery() ;
                    
                    if( rs2 != null && rs2.next() )
                    {
                        component.setInceptionPM( Double.parseDouble( rs2.getString( 1 ) == null ? "0" : rs2.getString( 1 ) ) );
                        component.setElaborationPM( Double.parseDouble( rs2.getString( 2 ) == null ? "0" : rs2.getString( 2 ) ) );
                        component.setConstructionPM( Double.parseDouble( rs2.getString( 3 ) == null ? "0" : rs2.getString( 3 ) ) );
                        component.setTransitionPM( Double.parseDouble( rs2.getString( 4 ) == null ? "0" : rs2.getString( 4 ) ) );
                    }
                    
                    sql = COINCOMOComponentManager.get_All_Phase_Schedule( component.getUnitID() ) ;
                    preparedStatement = (PreparedStatement) connection.prepareStatement( sql );
                    rs2 = preparedStatement.executeQuery() ;
                    
                    if( rs2 != null && rs2.next() )
                    {
                        component.setInceptionM( Double.parseDouble( rs2.getString( 1 ) == null ? "0" : rs2.getString( 1 ) ) ) ;
                        component.setElaborationM( Double.parseDouble( rs2.getString( 2 ) == null ? "0" : rs2.getString( 2 ) ) );
                        component.setConstructionM( Double.parseDouble( rs2.getString( 3 ) == null ? "0" : rs2.getString( 3 ) ) );
                        component.setTransitionM( Double.parseDouble( rs2.getString( 4 ) == null ? "0" : rs2.getString( 4 ) ) );
                    }
                    
                    // Load Using the same Connection for efficiency ...
                    COINCOMOComponentManager.loadComponent( component , connection ) ;
                    
                    subSystem.addSubUnit( component );
                }

                // Release From Memory
                preparedStatement.close();
            }
            catch ( SQLException e )
            {
                // Print any Problems ...
                e.printStackTrace();
                
                return false ;
            }
        }

        return true ;
    }

    
    public static boolean deleteSubComponents( Vector<COINCOMOSubSystem> SubComponents )
    {
        boolean isSuccessful = false;

        // If Exists
        if ( SubComponents != null && !SubComponents.isEmpty() )
        {
            DBConnection connection = (DBConnection) COINCOMODatabaseManager.getConnection();

            // Check if connection exists
            if ( connection != null )
            {
                try
                {
                    String sql = "delete from cocomo_SubSystems_table where SubSystem_ID=?";

                    // Efficent Deletion
                    PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement( sql );

                    for ( int i = 0 ; i < SubComponents.size() ; i++ )
                    {
                        COINCOMOSubSystem SubComponent = SubComponents.get( i );

                        // Replace "?" With ID Value
                        preparedStatement.setLong( 1 , SubComponent.getUnitID() );

                        // Delete
                        preparedStatement.executeUpdate();
                    }

                    // Release From Memory
                    preparedStatement.close();

                    // Update Status
                    isSuccessful = true;
                }
                catch ( SQLException e )
                {
                    // Print any Problems ...
                    e.printStackTrace();
                }
                finally
                {
                    // Disconnect
                    COINCOMODatabaseManager.disconnect( connection );
                }
            }
        }

        return isSuccessful;
    }
}
